package com.hice.demo.java.executor;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * ScheduledThreadPool测试
 * 创建一个支持定时及周期性的任务执行的线程池，多数情况下可用来替代Timer类。
 *
 * @author hyson
 * @create 2017-03-06 16:38
 */
public class ScheduledThreadPoolTest {

    private static Executor executor = Executors.newFixedThreadPool(10);
    private static Runnable task = () -> System.out.println("task over");
//    private static Runnable task = new Runnable() {
//        @Override
//        public void run() {
//            System.out.println("task over");
//        }
//    };

    public static void main(String[] args) {
        executor.execute(task);

        executor = Executors.newScheduledThreadPool(10);
        ScheduledExecutorService scheduler = (ScheduledExecutorService) executor;
        scheduler.scheduleAtFixedRate(task, 10, 10, TimeUnit.SECONDS);
    }

}
